home *** CD-ROM | disk | FTP | other *** search
/ Programming Sound Cards / Programming Sound Cards.iso / sound_57 / mousfunc.pas < prev    next >
Pascal/Delphi Source File  |  1995-01-01  |  8KB  |  289 lines

  1.  
  2. { Functions for MSmouse and Turbo Pascal 4             Rowan McKenzie 28/3/89}
  3.  
  4. Unit mousfunc;
  5.  
  6. Interface
  7.  
  8. Uses crt, dos, turbmous, graph;
  9.  
  10. Const
  11.   arrowxsize     = 11;            {width of arrow pointer}
  12.   arrowysize     = 17;            {height       "         }
  13.  
  14. Var
  15.   mousex, mousey : Integer;
  16.   mouseexists    : Boolean;
  17.   arrowcolor     : Word;
  18.  
  19. Function mouseinit : Boolean;
  20.   { Initialise mouse, return true if mouse available}
  21.  
  22. Procedure initpointer;
  23.   { initialise mouse arrow pointer (must be graphics mode)}
  24.  
  25. Procedure mousearrowon;
  26.   { Plots an arrow pointer at x,y}
  27.  
  28. Procedure mousearrowoff;
  29.   { Removes last arrow pointer}
  30.  
  31. Function mousemoved : Boolean;
  32.   { Checks for movement of mouse, if true, updates x,y}
  33.  
  34. Function mousekeys : Byte;
  35. { returns mouse key status in byte
  36.     eg bit 0 for left key
  37.        bit 1 for right key
  38.        bit 2 for centre key         }
  39.  
  40.  
  41. Procedure updatemousepos;
  42.  
  43.   { limit mouse movement and replot in new position}
  44.  
  45. Function trackmouse : Char;
  46.   { plot mouse arrow until mouse key pressed, keypress interrupts}
  47.  
  48. Implementation
  49.  
  50. Const
  51.   arrowpoints    = 10;            {no. points in arrow}
  52.  
  53.   uparrowshape : Array[1..arrowpoints] Of pointtype =
  54.   ((x : 0; y : 0), (x : 0; y : 13), (x : 3; y : 10), (x : 6; y : 16),
  55.    (x : 8; y : 16), (x : 8; y : 15), (x : 6; y : 9), (x : 10; y : 9),
  56.    (x : 1; y : 0), (x : 0; y : 0));
  57.  
  58. Var
  59.   arrowpointers  : Array[1..arrowxsize] Of Pointer;
  60.   mousexold, mouseyold,
  61.   mouselastx, mouselasty : Integer; {last x,y of mouse arrow for erase}
  62.  
  63.  
  64.  
  65.   Function mouseinit : Boolean;
  66.  
  67.     { Initialise mouse, return true if mouse available}
  68.  
  69.   Begin                           {mouseinit}
  70.     mouseexists := False;
  71.     If msmouse Then
  72.     Begin
  73.       mouseexists := True;
  74.       reset_mouse;
  75.       mouseinit := True;
  76.     End
  77.     Else
  78.       mouseinit := False;
  79.   End;                            {mouseinit}
  80.  
  81.  
  82.  
  83.   Procedure initpointer;
  84.  
  85.     { initialise mouse arrow pointer (must be graphics mode)}
  86.  
  87.   Var i          : Integer;
  88.  
  89.   Begin                           {initpointer}
  90.     arrowcolor := getmaxcolor;
  91.     mousex := getmaxx Div 2;      {start mouse in screen centre}
  92.     mousey := getmaxy Div 2;
  93.     mouselastx := mousex;
  94.     mouselasty := mousey;
  95.     mousexold := mousex;
  96.     mouseyold := mousey;
  97.     drawpoly(arrowpoints, uparrowshape); {draw arrow}
  98.     fillpoly(arrowpoints, uparrowshape);
  99.     For i := 1 To arrowxsize Do
  100.     Begin
  101.       GetMem(arrowpointers[i], imagesize(0, 0, arrowxsize-1, arrowysize));
  102.       getimage(0, 0, i-1, arrowysize, arrowpointers[i]^); {save image}
  103.     End;
  104.     cleardevice;
  105.   End;                            {initpointer}
  106.  
  107.  
  108.  
  109.   Procedure mousearrowon;
  110.  
  111.     { Plots an arrow pointer at mousex,mousey}
  112.  
  113.   Var viewport   : viewporttype;
  114.  
  115.   Begin                           {mousearrowon}
  116.     getviewsettings(viewport);
  117.     setviewport(0, 0, getmaxx, getmaxy, True);
  118.     If mousey = getmaxy Then      {puimage doesn't work on last line!}
  119.     Begin
  120.       putpixel(mousex, mousey, getmaxcolor-getpixel(mousex, mousey));
  121.       putpixel(Succ(mousex), mousey, getmaxcolor-getpixel(Succ(mousex), mousey));
  122.     End
  123.     Else
  124.       If mousex <= getmaxx-Pred(arrowxsize) Then
  125.         putimage(mousex, mousey, arrowpointers[arrowxsize]^, xorput)
  126.       Else
  127.         putimage(mousex, mousey, arrowpointers[getmaxx-Pred(mousex)]^, xorput);
  128.     setviewport(viewport.x1, viewport.y1, viewport.x2, viewport.y2,
  129.                 viewport.clip);
  130.     mouselastx := mousex;
  131.     mouselasty := mousey;
  132.   End;                            {mousearrowon}
  133.  
  134.  
  135.   Procedure mousearrowoff;
  136.  
  137.     { Removes last arrow pointer}
  138.  
  139.   Var
  140.     viewport       : viewporttype;
  141.  
  142.   Begin                           {mousearrowoff}
  143.     getviewsettings(viewport);
  144.     setviewport(0, 0, getmaxx, getmaxy, True);
  145.     If mouselasty = getmaxy Then  {puimage doesn't work on last line!}
  146.     Begin
  147.       putpixel(mouselastx, mouselasty,
  148.                getmaxcolor-getpixel(mouselastx, mouselasty));
  149.       putpixel(Succ(mouselastx), mouselasty,
  150.                getmaxcolor-getpixel(Succ(mouselastx), mouselasty));
  151.     End
  152.     Else
  153.       If mouselastx <= getmaxx-Pred(arrowxsize) Then
  154.         putimage(mouselastx, mouselasty, arrowpointers[arrowxsize]^, xorput)
  155.       Else
  156.         putimage(mouselastx, mouselasty,
  157.                  arrowpointers[getmaxx-Pred(mouselastx)]^, xorput);
  158.     setviewport(viewport.x1, viewport.y1, viewport.x2, viewport.y2,
  159.                 viewport.clip);
  160.   End;                            {mousearrowoff}
  161.  
  162.  
  163.   Function mousemoved : Boolean;
  164.  
  165.     { Checks for movement of mouse, if true, updates mousex,y}
  166.  
  167.   Var xinc, yinc : Integer;
  168.  
  169.   Begin                           {mousemoved}
  170.     If mouseexists Then
  171.     Begin
  172.       mouse_motion(xinc, yinc);
  173.       If (xinc <> 0) Or (yinc <> 0) Then
  174.       Begin
  175.         mousemoved := True;
  176.         mousex := mousex+xinc;
  177.         mousey := mousey+yinc;
  178.       End
  179.       Else
  180.         mousemoved := False;
  181.     End
  182.     Else
  183.       mousemoved := False;
  184.   End;                            {mousemoved}
  185.  
  186.  
  187.   Function mousekeys : Byte;
  188.  
  189. { returns mouse key status in byte
  190.     eg bit 0 for left key
  191.        bit 1 for right key
  192.        bit 2 for centre key
  193.  
  194.   keyboard equivalents are Alt  for left button
  195.                            Ctrl for centre button
  196.                            caps for right button  }
  197.  
  198.   Var dummy, keys : Integer;
  199.  
  200.   Begin                           {mousekeys}
  201.     keys := 0;
  202.     If (mem[$0:$417] And 12 > 0) Or (mem[$0:$418] And 64 > 0) 
  203.     Or Not mouseexists Then       {if one of three keys down}
  204.     Begin
  205.       If mem[$0:$418] And 64 > 0 Then {caps lock}
  206.         keys := keys+2;
  207.       If mem[$0:$417] And 8 > 0 Then {alt key}
  208.         keys := keys+1;
  209.       If mem[$0:$417] And 4 > 0 Then {ctrl}
  210.         keys := keys+4;
  211.     End
  212.     Else
  213.       get_mouse_status(keys, dummy, dummy);
  214.     mousekeys := keys;
  215.   End;                            {mousekeys}
  216.  
  217.  
  218.   Procedure updatemousepos;
  219.  
  220.     { limit mouse movement and replot in new position}
  221.  
  222.   Begin                           {updatemousepos}
  223.     If mousex > getmaxx Then
  224.       mousex := getmaxx;
  225.     If mousex < 0 Then
  226.       mousex := 0;
  227.     If mousey > getmaxy Then
  228.       mousey := getmaxy;
  229.     If mousey < 0 Then
  230.       mousey := 0;
  231.     mousearrowoff;
  232.     mousexold := mousex;
  233.     mouseyold := mousey;
  234.     mousearrowon;                 {arrow on}
  235.   End;                            {updatemousepos}
  236.  
  237.  
  238.   Function trackmouse : Char;
  239.  
  240.     { plot mouse arrow until mouse key pressed, keypress interrupts}
  241.  
  242.   Var c          : Char;
  243.  
  244.   Begin                           {trackmouse}
  245.     updatemousepos;           {incase movement since last time this was called}
  246.     c := ' ';
  247.     Repeat
  248.       If keypressed Then
  249.         c := readkey;
  250.     Until (mousekeys = 0) Or (c = ^c); {make sure buttons released}
  251.     While keypressed Do           {flush kbd}
  252.       c := readkey;
  253.     If c <> ^c Then
  254.       While (mousekeys = 0) And (c = ' ') Do
  255.       Begin
  256.         If keypressed Then
  257.         Begin
  258.           c := readkey;
  259.           Case c Of
  260.             #0 : Begin
  261.                    c := readkey;
  262.                    Case c Of
  263.                      #72 : Begin mousey := mousey-10; c := ' '; End;
  264.                      #80 : Begin mousey := mousey+10; c := ' '; End;
  265.                      #75 : Begin mousex := mousex-10; c := ' '; End;
  266.                      #77 : Begin mousex := mousex+10; c := ' '; End;
  267.                    End;           {case}
  268.                  End;
  269.             '8' : Begin Dec(mousey); c := ' '; End;
  270.             '2' : Begin Inc(mousey); c := ' '; End;
  271.             '4' : Begin Dec(mousex); c := ' '; End;
  272.             '6' : Begin Inc(mousex); c := ' '; End;
  273.           End;                    {case}
  274.           updatemousepos;
  275.         End;
  276.         If mousemoved Then
  277.           updatemousepos;
  278.       End;
  279.     If c <> ' ' Then
  280.       trackmouse := c
  281.     Else
  282.       trackmouse := #0;
  283.   End;                            {trackmouse}
  284.  
  285.  
  286. Begin
  287.   mouseexists := False;
  288. End.
  289.